package org.trebor.sandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import org.trebor.sandroid.World;

import static android.util.Log.*;
import static android.graphics.Color.*;
import static android.graphics.Paint.*;

public class Sandroid extends Activity 
{
    private static final String TAG = "Sandroid";

    World world;

    private Paint p = new Paint()
      {
          {
            setAntiAlias(false);
            setColor(RED);
          }
      };


    /** Called when the activity is first created. */

    View myView = null;
    Thread animationThread = new Thread()
      {
          int count = 1;
          int clr = 0xffffff;
          public void run()
          {
            while (true)
            {
              try
              {
                sleep(50);
                d(TAG, "start update: " + count);
                world.update();
                d(TAG, "end update: " + count++);
                myView.postInvalidate();
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
            }
          }
      };


    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      
      myView = new View(this)
        {
            int x = 0;
            int y = 0;

            @Override
            protected void onDraw(Canvas c)
            {
              if (world == null)
              {
                world = new World(
                  c.getClipBounds().width(), c.getClipBounds().height());
                world.initialize();
              }
              
              //super.onDraw(c);
              //c.drawColor(WHITE);
              //c.drawOval(new RectF(c.getClipBounds()), p);
              world.paint(c);
              //c.drawRect(c.getClipBounds(), p);
              //c.drawRect(new Rect(x, y, x + 20, y + 20), p);
              //x += 5;
              //y += 5;
              //d(TAG, "onDraw");
            }
        };

      setContentView(myView);
      animationThread.start();
    }
}
